load("@rules_cc//cc:defs.bzl", "cc_test")
load("//tools:cc.bzl", "COPTS")
load("//tools:transition.bzl", "PLATFORM", "transition_default_constraints")

# TODO: Handle conf when enable_prof = True
#{ "src": "batch_alloc_prof.c" },
#{ "src": "arena_reset_prof.c" },
UNIT_TESTS = [
    {"src": "a0.c"},
    {
        "src": "arena_decay.c",
        "conf": "dirty_decay_ms:1000,muzzy_decay_ms:1000,tcache_max:1024",
    },
    {"src": "arena_reset.c"},
    {"src": "atomic.c"},
    {"src": "background_thread.c"},
    {"src": "background_thread_enable.c"},
    {"src": "base.c"},
    {"src": "batch_alloc.c"},
    {
        "src": "binshard.c",
        "conf": "narenas:1,bin_shards:1-160:16|129-512:4|256-256:8",
    },
    {"src": "bitmap.c"},
    {"src": "bit_util.c"},
    {"src": "buf_writer.c"},
    {"src": "cache_bin.c"},
    {"src": "ckh.c"},
    {"src": "counter.c"},
    {"src": "decay.c"},
    {"src": "div.c"},
    {"src": "double_free.c"},
    {"src": "edata_cache.c"},
    {"src": "emitter.c"},
    {"src": "extent_quantize.c"},
    {"src": "fb.c"},
    {"src": "fork.c"},
    {"src": "fxp.c"},
    {
        "src": "san.c",
        "conf": "san_guard_large:1,san_guard_small:1",
    },
    {"src": "san_bump.c"},
    {"src": "hash.c"},
    {"src": "hook.c"},
    {"src": "hpa.c"},
    {
        "src": "hpa_background_thread.c",
        "conf": "hpa_dirty_mult:0,hpa_min_purge_interval_ms:50,hpa_sec_nshards:0",
    },
    {"src": "hpdata.c"},
    {"src": "huge.c"},
    {"src": "inspect.c"},
    {
        "src": "junk.c",
        "conf": "abort:false,zero:false,junk:true",
    },
    {
        "name": "unit_junk_alloc",
        "src": "junk.c",
        "conf": "abort:false,zero:false,junk:alloc",
    },
    {
        "name": "unit_junk_free",
        "src": "junk.c",
        "conf": "abort:false,zero:false,junk:free",
    },
    {"src": "log.c"},
    {"src": "mallctl.c"},
    {"src": "malloc_conf_2.c"},
    {"src": "malloc_io.c"},
    {"src": "math.c"},
    {"src": "mpsc_queue.c"},
    {"src": "mq.c"},
    {"src": "mtx.c"},
    {"src": "nstime.c"},
    {"src": "oversize_threshold.c"},
    {"src": "pa.c"},
    {"src": "pack.c"},
    {"src": "pages.c"},
    {"src": "peak.c"},
    {"src": "ph.c"},
    {"src": "prng.c"},
    {"src": "prof_accum.c"},
    {"src": "prof_active.c"},
    {"src": "prof_gdump.c"},
    {"src": "prof_hook.c"},
    {"src": "prof_idump.c"},
    {"src": "prof_log.c"},
    {"src": "prof_mdump.c"},
    {"src": "prof_recent.c"},
    {"src": "prof_reset.c"},
    {"src": "prof_stats.c"},
    {"src": "prof_tctx.c"},
    {"src": "prof_thread_name.c"},
    {"src": "prof_sys_thread_name.c"},
    {"src": "psset.c"},
    {"src": "ql.c"},
    {"src": "qr.c"},
    {"src": "rb.c"},
    {"src": "retained.c"},
    {"src": "rtree.c"},
    {"src": "safety_check.c"},
    {"src": "sc.c"},
    {"src": "sec.c"},
    {"src": "seq.c"},
    {"src": "SFMT.c"},
    {"src": "size_check.c"},
    {"src": "size_classes.c"},
    {"src": "slab.c"},
    {"src": "smoothstep.c"},
    {"src": "spin.c"},
    {"src": "stats.c"},
    {"src": "stats_print.c"},
    {"src": "sz.c"},
    {
        "src": "tcache_max.c",
        "conf": "tcache_max:1024",
    },
    {"src": "test_hooks.c"},
    {"src": "thread_event.c"},
    {"src": "ticker.c"},
    {"src": "tsd.c"},
    {"src": "uaf.c"},
    {"src": "witness.c"},
    {
        "src": "zero.c",
        "conf": "abort:false,junk:false,zero:true",
    },
    {
        "src": "zero_realloc_abort.c",
        "conf": "zero_realloc:abort",
    },
    {
        "src": "zero_realloc_free.c",
        "conf": "zero_realloc:free",
    },
    {
        "src": "zero_realloc_alloc.c",
        "conf": "zero_realloc:alloc",
    },
    {
        "src": "zero_reallocs.c",
        "conf": "zero_realloc:free",
    },
]

transition_default_constraints(
    name = "testlib",
    testonly = True,
    src = "//test:testlib",
    platform = PLATFORM,
    settings = {
        "//settings:enable_jet": "True",
        "//settings:with_test": "unit",
        "//settings/flags:enable_fill": "True",
        # This needs to be pinned for testing. The MALLOC_CONF ENV depends on it. The make build allows for it to be
        # set dynamically via templating and generates a test harness around it. By requiring a value that cannot be
        # set internally by accident, this exercises the same functionality.
        "//settings/flags:jemalloc_prefix": "je_test_",
    },
    visibility = ["//:__subpackages__"],
)

[cc_test(
    name = test["name"] if "name" in test else test["src"].removesuffix(".c").replace("/", "_"),
    srcs = [test["src"]],
    copts = COPTS,
    env = {"JE_TEST_MALLOC_CONF": test.get("conf", "")},
    linkstatic = True,
    local_defines = ["JEMALLOC_UNIT_TEST"],
    deps = [":testlib"],
) for test in UNIT_TESTS]
